﻿using System;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
using System.Windows.Forms;
using EncryptionLib;
using EncryptionLib.Encryption;
using EncryptionLib.Signature;
using EncryptionLib.Users;
using System.Text;
using System.Collections.Generic;

namespace TotalCommander_v3
{
    public partial class frmEncryptOption : Form
    {
        public SymmetricAlgorithms SymmetricAlgorithms = SymmetricAlgorithms.DES;
        public CipherMode CipherMode = CipherMode.ECB;
        public PaddingMode PaddingMode = PaddingMode.PKCS7;
        public bool Zip = true;
        public string sFileOutput = String.Empty;
        public string sEmail = String.Empty;
        public bool canZip = true;

        public frmEncryptOption()
        {
            InitializeComponent();
            cmbEncryptAlgorithm.Items.Add(SymmetricAlgorithms.DES);
            cmbEncryptAlgorithm.Items.Add(SymmetricAlgorithms.RC2);
            cmbEncryptAlgorithm.Items.Add(SymmetricAlgorithms.Rijndael);
            cmbEncryptAlgorithm.Items.Add(SymmetricAlgorithms.TripleDES);
            cmbEncryptAlgorithm.SelectedIndex = 0;

            cmbEncryptCipherMode.Items.Add(CipherMode.ECB);
            cmbEncryptCipherMode.Items.Add(CipherMode.CBC);
            cmbEncryptCipherMode.Items.Add(CipherMode.CFB);
            cmbEncryptCipherMode.Items.Add(CipherMode.CTS);
            cmbEncryptCipherMode.Items.Add(CipherMode.OFB);
            cmbEncryptCipherMode.SelectedIndex = 0;

            cmbEncryptPaddingMode.Items.Add(PaddingMode.ANSIX923);
            cmbEncryptPaddingMode.Items.Add(PaddingMode.ISO10126);
            cmbEncryptPaddingMode.Items.Add(PaddingMode.None);
            cmbEncryptPaddingMode.Items.Add(PaddingMode.PKCS7);
            cmbEncryptPaddingMode.Items.Add(PaddingMode.Zeros);
            cmbEncryptPaddingMode.SelectedIndex = 0;
        }

        private void frmEncprytOption_Load(object sender, EventArgs e)
        {
            List<UserData> lUser = new List<UserData>();
            for (int i = 0; i < frmMain.m_userManager.GetCount(); i++)
            {
                lUser.Add(frmMain.m_userManager.GetUser(i));
            }

            if (lUser.Count > 0)
                this.cbReceiver.DataSource = lUser;
            else
            {
                MessageBox.Show("You don't have any receivers!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.btnEncrypt.Enabled = false;
            }

            this.tbOutputName.Text = sFileOutput;
            if (canZip)
                chkEncryptZip.Enabled = true;
            else
                chkEncryptZip.Enabled = false;
        }

        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            this.SymmetricAlgorithms = (SymmetricAlgorithms)cmbEncryptAlgorithm.SelectedItem;
            this.CipherMode = (CipherMode) this.cmbEncryptCipherMode.SelectedItem;
            this.PaddingMode = (PaddingMode) this.cmbEncryptPaddingMode.SelectedItem;
            this.Zip = chkEncryptZip.Checked;
            this.sEmail = cbReceiver.SelectedValue.ToString();
            this.sFileOutput = tbOutputName.Text;

            if (File.Exists(this.sFileOutput))
            {
                MessageBox.Show("Can not encrypt! Destination file already exists\nChoose Encrypt with... to change file name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string [] arrTemp = tbOutputName.Text.Split('\\');
            string sFileName = arrTemp[arrTemp.GetLength(0)-1];

            FolderBrowserDialog folder = new FolderBrowserDialog();
            if (folder.ShowDialog() == DialogResult.OK)
            {
                tbOutputName.Text = folder.SelectedPath.ToString() + "\\" + sFileName;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

      
    }
}
